package com.csaba.connector.model;

import com.csaba.connector.ServiceException;

/**
 * Represents a session to a banking server. 
 * <p> A Session instance is required to invoke any bank services, so this can 
 * be used to store session state information to use across the bank service
 * instances between a login and logout.
 * <p> Normally here the remote properties are not coming from the bank, but can be used
 * as a cahce, or session memory, for storing and retrieving information out of it.
 * <p> Session objects are not instantiated directly, but the LoginService is responsible
 * for producing one on succesful authentication, hence the LoginService is the only one, 
 * that can be executed with "null" session.
 * @see com.csaba.connector.service.LoginService
 * @author Gabe
 *
 */
@SuppressWarnings( "serial" )
public class Session extends AbstractRemoteObject
{
	private Bank bank;
	private Customer customer;
	private String sessionId;

	private int dummyInterval;

	protected boolean activeSession = true;

	public void invalidateSession() throws ServiceException
	{
		if ( !activeSession )
			throw new ServiceException("Session is already invalidated. SessionID = " + sessionId);//FIXME I18N
		activeSession = false;
	}

	public Bank getBank()
	{
		return bank;
	}

	public void setBank( final Bank bank )
	{
		this.bank = bank;
	}

	public Customer getCustomer()
	{
		return customer;
	}

	public void setCustomer( final Customer customer )
	{
		this.customer = customer;
	}

	public String getSessionId()
	{
		return sessionId;
	}

	public void setSessionId( final String sessionId )
	{
		this.sessionId = sessionId;
	}

	@Override
	public String toString()
	{
		return "session[" + bank + ";" + sessionId + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}

	/**
	 * Sets dummy interval for this session. Dummy interval specifies, how frequently the 
	 * dummy message should be sent. Dummy message is sent by the {@link com.csaba.connector.DummyServer}.
	 * <p>By default this interval is zero, that means no dummy request is required.
	 * @param dummyInterval Frequency of the dummy requests specified in seconds.
	 */
	public void setDummyInterval( final int dummyInterval )
	{
		this.dummyInterval = dummyInterval;
	}

	/**
	 * Returns dummy interval for this session. Dummy interval specifies, how frequently the 
	 * dummy message should be sent. Dummy message is sent by the {@link com.csaba.connector.DummyServer}.
	 * @param dummyInterval Frequency of the dummy requests specified in seconds.
	 */
	public int getDummyInterval()
	{
		return dummyInterval;
	}

	@Override
	public boolean equals( final Object obj )
	{
		boolean same = true;
		if ( obj == this )
		{
			same = true;
		}
		else if ( obj != null && obj instanceof Session )
		{
			final Session other = (Session) obj;
			same = true;
			if ( ( other.sessionId == null && sessionId != null ) || other.sessionId.equals(sessionId) )
				same = false;
			if ( ( other.bank == null && bank != null ) || other.bank.equals(bank) )
				same = false;
		}
		else
		{
			same = false;
		}
		return same;
	}

	public boolean isActiveSession()
	{
		return activeSession;
	}

}
